import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import zip from 'rollup-plugin-zip';
import copy from 'rollup-plugin-copy2';
import _copy from 'rollup-plugin-copy';

const { npm_package_name, npm_package_version } = process.env;

const bundle = `${npm_package_name}-${npm_package_version}.zip`;

export default defineConfig({
  base: './',
  server: {
    port: 3050,
  },
  build: {
    target: 'esnext'
  },
  plugins: [
    svelte({}),
    copy({
      assets: ['manifest.json'],
    }),
    zip({
      file: bundle,
    }),
    _copy({
      targets: [
        {
          src: ['dist/*', '!**/*.zip'],
          dest: `../../src-tauri/extensions/${npm_package_name}`,
        },
        { src: `dist/${bundle}`, dest: `bundle` },
      ],
      verbose: true,
      hook: 'writeBundle',
    }),
  ],
});
